package net.asfun.jvalog.mvc;

import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HandlerController {
	
	private Method method;
	private Object controller;
	
	public HandlerController(Object controller, Method method) throws Exception{
		this.method = method;
		this.controller = controller;
	}

	public ModelAndView handler(Class<ModelAndView> mavClass, HttpServletRequest req, HttpServletResponse resp) throws Exception{
		ModelAndView mav = mavClass.newInstance();
		Map<String, Object> model = mav.getModel();
		String name = (String) method.invoke(controller, req, model);
		mav.setName(name);
		return mav;
	}
	
	@Override
	public String toString() {
		return "HandlerController [" + controller.getClass().getName() + "." + method.getName() + "]";
	}
	
}
